{extend name="layout" /}
{block name="content"}
<div class="layui-tab layui-tab-brief">
	<ul class="layui-tab-title">
	    <li class="layui-this">{$pathCurrent['name']}</li>
	    <li><a href="{:url('option')}">添加分配记录</a></li>
  	</ul>
</div>
<div class="demoTable" style="margin-top: 25px;margin-left:20px;">
	建单时间：
	<div class="layui-inline">
		<input type="text" class="layui-input" id="start_time" name="start_time" placeholder="yyyy-MM-dd HH:mm:ss">
	</div>
	-
	<div class="layui-inline">
		<input type="text" class="layui-input" id="end_time" name="end_time" placeholder="yyyy-MM-dd HH:mm:ss">
	</div>
	<br>
	<br>

	产品：
	<div class="layui-inline">
		<form class="layui-form">
			<select class="layui-input" name="goods_id" id="goods_id" lay-filter="goods_id">
				<option value="">请选择产品</option>
				{foreach $goods as $vo}
				<option value="{$vo['id']}">{$vo['title']}</option>
				{/foreach}
			</select>
		</form>
	</div>
	业务员：
	<div class="layui-inline">
		<form class="layui-form">
			<select class="layui-input" name="customer_id" id="customer_id" lay-filter="customer_id">
				<option value="">请选择业务员：</option>
				{foreach $customer as $vo}
				<option value="{$vo['id']}">{$vo['customer_name']}</option>
				{/foreach}
			</select>
		</form>
	</div>
	备注：
	<div class="layui-inline" style="width:200px;">
		<input class="layui-input" name="like" id="like" placeholder="备注" autocomplete="off">
	</div>
	<button class="layui-btn" data-type="reload">搜索</button>
</div>
<div class="layui-card-body">
	<div data-model="table-bind">
		<table class="layui-hide" id="data_table" lay-filter="data_table" data-table></table>
	</div>
</div>
<script type="text/html" id="barTar">
  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" data-del data-id="{{d.id}}" data-table="user">删除</a>
</script>
<script type="text/html" id="image">
	<img src="{{d.image}}" height="30">
</script>
<script type="text/html" id="sysTar">
	{{#  if(d.status == 1){ }}
  	<button type="button" class="layui-btn layui-btn-normal">启用</button>
  	{{#  } else { }}
  	<button type="button" class="layui-btn layui-btn-danger">冻结</button>
  	{{#  } }}
</script>
{/block}
{block name="js"}
<script type="text/javascript">
var mixin = {};
var SlefUrl = this.location.href;
layui.use(['table','upload','laydate'], function(){
	var table = layui.table;
    var upload = layui.upload;
    var laydate = layui.laydate;
	table.render({
		elem: '#data_table',
		url:SlefUrl,
		limit:20,
		cellMinWidth: 80,
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
		title: '导出Excel'
		,layEvent: 'LAYTABLE_Excel'
		,icon: 'layui-icon layui-icon-export'
        }]
		,cols: [[
	      	{field:'id',width:80, title: 'ID', sort: true,align:'center'}
	      	,{field:'goods_text', title: '分配产品', width:80,align:'center'}
	      	,{field:'customer_name', title: '业务员名称'}
            ,{field:'count_num', title: '分配数量',width:100}
            ,{field:'number_line', title: '流水号范围'}
            ,{field:'remark', title: '备注',width:100}
	      	,{field:'timestamp', title: '分配时间',width:150}
	      	,{field:'op', title: '操作',toolbar: '#barTar', width:200,align:'center'}
	    ]],
	    page: true,
        id: 'testReload'
	});
    //日期时间选择器
    laydate.render({
        elem: '#start_time'
        ,type: 'datetime'
    });
    laydate.render({
        elem: '#end_time'
        ,type: 'datetime'
    });

    //头工具栏事件
    table.on('toolbar(data_table)', function(obj){
        // 数据导出
        if(obj.event === 'LAYTABLE_Excel'){
            var result = {};
            result.customer_id = $('#customer_id').val();
            result.goods_id = $('#goods_id').val();
            result.like = $('#like').val();
            result.start_time = $('#start_time').val();
            result.end_time = $('#end_time').val();

            DownLoadFile({'url':''+webRoot+ webControl+'/load_excel','data':result})
        }

    });

	table.on('tool(data_table)', function(obj){
		var data = obj.data;
		data.table = "staff";
		if(obj.event === 'del'){
			layer.confirm('确定删除吗？', function(index){
	        	layer.close(index);
	       		form.formDel(data,obj);
	      	});
		}



	});
    var $ = layui.$, active = {
        reload: function(){
            var customer_id = $('#customer_id');
            var goods_id = $('#goods_id');
            var like = $('#like');
            var start_time = $('#start_time');
            var end_time = $('#end_time');

            //执行重载
            table.reload('testReload', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    customer_id: customer_id.val(), goods_id: goods_id.val(),like:like.val(),start_time:start_time.val(),end_time:end_time.val()
                }
            }, 'data');
        },
		excelDemo: function(){
            window.location.href = '/excel/product_demo.xls'
        }
    };

	upload.render({
		elem: '#uploadExcel'
		,url: '{:url("savestudentImport")}'
		,accept: 'file' //普通文件
		,auto:'xlsx|xls|csv'
		,done: function(res){
			layer.alert(res.msg);
			// setInterval(function(){
			// 	window.location.reload();
			// },3000)
			
		}
	});

    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

	$('#excelDemo').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });




});

var DownLoadFile = function (options) {
    var config = $.extend(true, { method: 'post' }, options);
    var $iframe = $('<iframe id="down-file-iframe" />');
    var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
    $form.attr('action', config.url);
    for (var key in config.data) {
        $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
    }
    $iframe.append($form);
    $(document.body).append($iframe);
    $form[0].submit();
    $iframe.remove();
}
</script>
{/block}